package com.example.practice.ui;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.view.MenuItem;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.cinema.CinemaFragment;
import com.example.home.ui.HomeFragment;
import com.example.live.LiveFragment;
import com.example.mine.MineFragment;
import com.example.mvi_core.model.BaseActivity;
import com.example.common.ARouterPath;
import com.example.practice.MainAdapter;
import com.example.practice.R;
import com.example.publish.PublishFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;
@Route(path = ARouterPath.USER_JUMP_MAIN)
public class MainActivity extends BaseActivity {
private ViewPager vp_main;
private BottomNavigationView bottom_main;
private MainAdapter mainAdapter;


    @Nullable
    @Override
    public View getLayoutRootView() {
        return null;
    }

    @Override
    protected int getLayoutId(int layout) {
        return R.layout.activity_main;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void initView() {
        super.initView();
        vp_main=findViewById(R.id.page_main);
        bottom_main=findViewById(R.id.bottom);
        List<Fragment> list=getFragments();
        mainAdapter=new MainAdapter(getSupportFragmentManager(),list);
        vp_main.setAdapter(mainAdapter);
        bottom_main.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()){
                case R.id.home:
                    vp_main.setCurrentItem(0);
                    return true;
                case R.id.video:
                    vp_main.setCurrentItem(1);
                    return true;
                case R.id.send:
                    vp_main.setCurrentItem(2);
                    return true;
                case R.id.live:
                    vp_main.setCurrentItem(3);
                    return true;
                case R.id.mine:
                    vp_main.setCurrentItem(4);
                    return true;
            }
            return true;
        });
        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MenuItem item = bottom_main.getMenu().getItem(position);
                item.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private List<Fragment> getFragments(){
        List<Fragment> list=new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new CinemaFragment());
        list.add(new PublishFragment());
        list.add(new LiveFragment());
        list.add(new MineFragment());
        return list;
    }
}